<?php

include_once "src/controller/dbmanagement/DBManager.php";
include_once "src/controller/regmanagement/EmailManager.php";
include_once "src/regexception/DBConnectionException.php";
include_once "src/regexception/DBException.php";
include_once "src/regexception/GeneralRegistryException.php";

class AccountManager {

    public static function populateRegistry () {
        try {
            DBManager::populateRegistry();
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
    }

    public static function isEmailUnique($email) {
        $isEmailUnique = true;
        try {
            $isEmailUnique = DBManager::isEmailUnique($email);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $isEmailUnique;
    }

    public static function isValidLogIn($email, $password) {
        $isValidLogIn = true;
        try {
            $isValidLogIn = DBManager::isValidLogIn($email, $password);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $isValidLogIn;
    }

    public static function createUserAccount($firstName, $lastName, $email, $password) {
        try {
            DBManager::createUserAccount($firstName, $lastName, $email, $password);
            EmailManager::sendUserAccountCreationConfirmationEmail($firstName, $lastName, $email, $password);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
    }

    public static function getUserFullName($email) {
        $userFullName = "";
        try {
            $userFullName = DBManager::getUserFullName($email);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $userFullName;
    }

    public static function sendPasswordRecoveryEmail($email) {
        try {
            $user = AccountManager::getConsultant($email);
            EmailManager::sendPasswordRecoveryEmail($user->getFirstName(), $user->getLastName(), $user->getEmail(), $user->getPassword());
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return true;
    }

    public static function sendServiceCategoryToBeAddedEmail($email, $categoryName, $categoryDescription) {
        try {
            EmailManager::sendServiceCategoryToBeAddedEmail($email, $categoryName, $categoryDescription);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return true;
    }

    public static function getConsultant($email) {
        $user = NULL;
        try {
            $user = DBManager::getConsultant($email);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $user;
    }

    public static function updateUserProfile($consultant) {
        try {
            DBManager::updateUserProfile($consultant);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return true;
    }
 
    public static function updateUserAccount($consultantId, $oldEmail, $newEmail,
                                             $newFirstName, $newLastName, $newPassword,
                                             $oldPassword, $changePasswordIndicator) {
        try {

            if ($changePasswordIndicator == "1") {
                if (!DBManager::isValidLogIn($oldEmail, $oldPassword)) {
                    throw new GeneralRegistryException("InvalidLoginException");
                }
            }

            DBManager::updateUserAccount($consultantId, $oldEmail, $newEmail,
                                         $newFirstName, $newLastName, $newPassword,
                                         $oldPassword, $changePasswordIndicator);
                                     
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return true;
    }

    public static function deleteUserAccount($firstName, $lastName, $email, $accountCreationDate) {
        try {
            DBManager::deleteUserAccount($firstName, $lastName, $email, $accountCreationDate);

            EmailManager::sendUserAccountDeletionEmail($firstName, $lastName, $email);

        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return true;
    }

    public static function searchConsultants($searchGroupType, $consultantName, $email, $companyName, $servicesList, $pageNumber) {
        $consultants = array();
        try {
            $consultants = DBManager::searchConsultants($searchGroupType, $consultantName, $email, $companyName, $servicesList, $pageNumber);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return $consultants;
    }

}
?>
